package com.gy.util;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.Mode;
import cn.hutool.crypto.Padding;
import cn.hutool.crypto.digest.DigestUtil;
import cn.hutool.crypto.symmetric.AES;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class KeyUtil {

    /**
     * 密钥key
     */
    private static final String ENCODE_KEY = DigestUtil.md5Hex("5HJkA~NxC@A+*nyg@EBE"); //e2f1553123cdf69b63b2be6cfc5ef6c4

    /**
     * 偏移量。16 字节
     */
    private static final String IV_KEY = "4382822429223502";

    /**
     * 将加密字符串进行解密
     * @param data 加密字符串
     * @return 解密后的结果
     */
    public static String decryptFromString(String data) {
        AES aes = new AES(Mode.CBC, Padding.ISO10126Padding, ENCODE_KEY.getBytes(), IV_KEY.getBytes());
        return aes.decryptStr(Base64.getDecoder().decode(data), CharsetUtil.CHARSET_UTF_8);
    }

    /**
     * 将字符串进行加密
     * @param data 未加密的字符串
     * @return 加密后的密文结果
     */
    public static String encryptFromString(String data) {
        AES aes = new AES(Mode.CBC, Padding.ISO10126Padding, ENCODE_KEY.getBytes(), IV_KEY.getBytes());
        return aes.encryptBase64(data, StandardCharsets.UTF_8);
    }


    public static void main(String[] args) {
        System.out.println(Base64.getDecoder().decode(ENCODE_KEY));
        String encryptData = KeyUtil.encryptFromString("http://video.yujuyi.com/history/1730037292707364864/20231130_2.mp4");
        System.out.println("加密：" + encryptData);
        String decryptData = KeyUtil.decryptFromString(encryptData);
        System.out.println("解密：" + decryptData);
    }

}
